﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LogisticsBLL;
using System.Data;
using System.Text.RegularExpressions;

namespace WebLosgic.TruckTeam
{
    public partial class AddTruck : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ddlName();
                ddltype();
            }
        }

        public void ddlName()
        {
            TruckBLL tru = new TruckBLL();
            DataTable dt = tru.ddlbindCarName();
            DDLTeamName.DataSource = dt;
            DDLTeamName.DataTextField = "TeamName";
            DDLTeamName.DataValueField = "TeamID";
            DDLTeamName.DataBind();
            DDLTeamName.Items.Insert(0, new ListItem("请选择", "0"));
        }

        public void ddltype()
        {
            TruckBLL tru = new TruckBLL();
            DataTable dt = tru.ddlbindCarType();
            DDLType.DataSource = dt;
            DDLType.DataTextField = "Type";
            DDLType.DataValueField = "Type";
            DDLType.DataBind();
            DDLType.Items.Insert(0, new ListItem("请选择", "0"));
        }

        protected void BtnAdd_Click(object sender, EventArgs e)
        {
            
            string num = TextNumber.Text;
            
            string buydate = TextCheckTime.Text;
            string type = DDLType.SelectedValue;
            string length = TxtLenth.Text;
            string tonnger = txttrount.Text;
            int uid =Convert.ToInt32(DDLTeamName.SelectedValue);
            string remark = TxtRemark.Text;
            if (length == "" || tonnger == "" || num == "")
            {
                Page.ClientScript.RegisterStartupScript(GetType(), "aa", "alert('请完善信息')", true);
            }
            if (uid==0 )
            {
                Page.ClientScript.RegisterStartupScript(GetType(), "aa", "alert('请选择车队')", true);
            }
            if(type == "0")
            {
                Page.ClientScript.RegisterStartupScript(GetType(), "aa", "alert('请选择车辆类型')", true);
            }
            
            string NUmbercard = "^[冀豫云辽黑湘皖鲁苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼渝京津沪新京军空海北沈兰济南广成使领A-Z]{1}[a-zA-Z0-9]{5}[a-zA-Z0-9挂学警港澳]{1}$";
            Regex numreg = new Regex(NUmbercard);
         
            string validateNum = "^[0-9]+(.[0-9]{1,5})?$|^(0|[1-9][0-9]*)$";
            Regex valdate= new Regex(validateNum);
            if (valdate.IsMatch(TxtLenth.Text.Trim())==false || valdate.IsMatch(txttrount.Text.Trim()) == false)
            {
                Page.ClientScript.RegisterStartupScript(GetType(), "aa", "alert('车身长度和吨位必须为数字类型')", true);
            }
            if (numreg.IsMatch(TextNumber.Text.Trim()) == false)
            {
                Page.ClientScript.RegisterStartupScript(GetType(), "aa", "alert('车牌号码格式不正确，请重新输入')", true);
            }
            TruckBLL tru = new TruckBLL();
                int resut = tru.AddTruck(num, buydate, type, length, tonnger, uid, remark);
             if (resut > 0)
             {
                string name = Session["names"].ToString();
                string Behavor = "" + name + ": 添加车辆";
                int TypeId = 8;
                int UserID = Convert.ToInt32(Session["UserIDs"]);
                string Parameters =num+","+buydate+","+type+","+length+","+tonnger;
                string ProcName = "p_addTruck";
                HttpRequest request = HttpContext.Current.Request;
                string result = request.ServerVariables["HTTP_X_FORWARDED_FOR"];
                if (string.IsNullOrEmpty(result)) { result = request.ServerVariables["REMOTE_ADDR"]; }
                if (string.IsNullOrEmpty(result)) { result = request.UserHostAddress; }
                if (string.IsNullOrEmpty(result)) { result = "192.168.0.0"; }
                string IP = "192.168.0.1";
                int IsExcePion = 0;
                int a = ProcBLL.InserSys(Behavor, TypeId, UserID, Parameters, ProcName, IP, IsExcePion);
                if (a > 0)
                {
                    Page.ClientScript.RegisterStartupScript(GetType(), "aa", "alert('添加成功');location.href='CLxinxi.aspx'", true);

                }
            }
           else
           {
                    Page.ClientScript.RegisterStartupScript(GetType(), "aa", "alert('添加失败')", true);
           }


           
        }

        protected void BtnCong_Click(object sender, EventArgs e)
        {
            TextNumber.Text = "";
            DDLTeamName.SelectedValue = "0";
            TextCheckTime.Text = "";
            TxtLenth.Text = "";
            txttrount.Text = "";
            DDLTeamName.SelectedValue = "0";
            TxtRemark.Text = "";
        }

      
    }
}